<?php

namespace App\Http\Controllers\Admin;


use App\Models\Order;
use App\Models\OrderLease;
use App\Models\OrderLeasePartial;
use App\Models\OrderLog;
use App\Models\OrderRemark;
use App\Models\OtherWithholdLog;
use App\Models\ShopSms;
use App\Models\ShopTrans;
use App\Models\ShopUser;
use App\Models\SmsRecord;
use App\Models\SmsTemplate;
use App\Models\Source;
use App\Models\TransDetail;
use App\Services\AlipayService;
use App\Services\AntchainAtoService;
use App\Services\AntchainAtoServicejuzu;
use App\Services\AntchainService;
use App\Services\ChargeService;
use App\Services\CommonService;
use App\Services\ExportService;
use App\Services\OrderService;
use App\Services\RedisService;
use App\Services\SmsService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;

class FinanceController extends BaseController
{
    /**
     * 财务订单列表
     * @param Request $request
     * @return false|string
     */
    public function financeOrderList(Request $request){
        $data = Order::financeOrderList($request);
        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'获取成功',$data['list']);
    }

    /**
     * 查看扣款记录
     * @param Request $request
     * @return false|string
     */
    public function leaseWithholdLog(Request $request){
        $orderNo = $request->input('orderNo','');
        $leaseId = $request->input('leaseId','');
        $page = $request->input('page',1);
        $pageSize = $request->input('pageSize',10);
        if(!$orderNo || !$leaseId){
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'参数错误');
        }
        if(!Order::checkOrderByNo($orderNo)){
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'未找到订单信息');
        }

        $log = OtherWithholdLog::from('sp_other_withhold_log as log')
            ->where(['log.lease_id'=>$leaseId])
            ->leftjoin('sp_other_admin as a','a.id','log.admin_id')
            ->leftjoin('sp_order_lease_2 as ol','log.lease_id','ol.id')
            ->select(
                'log.status','log.trade_no','log.create_time','log.partial_amount','log.pay_type'
                ,'a.username'
                ,'ol.which_period','ol.begin_time','ol.end_time','ol.rent','ol.status as lease_status','log.merchant_contribute','log.result'
            )
            ->orderBy('log.id','desc')
            ->paginate($pageSize);
        $log = obToArray($log);
        if(isset($log['data']) && $log['data']){
            foreach ($log['data'] as $k=>$item){
                $log['data'][$k]['failReason'] = '';
                $log['data'][$k]['begin_time'] = dateDFormat($item['begin_time']);
                $log['data'][$k]['end_time'] = dateDFormat($item['end_time']);
                $log['data'][$k]['lease_status'] = OrderLease::ORDER_LEASE_STATUS_LIST[$item['lease_status']];
                $log['data'][$k]['pay_type'] = OtherWithholdLog::PAY_TYPE_LIST[$item['pay_type']];
                if($item['partial_amount']){
                    $log['data'][$k]['rent'] =$item['partial_amount'];
                }
                if($item['status'] == OtherWithholdLog::STATUS_SUCCESS){
                    $log['data'][$k]['pay_type'] = $log['data'][$k]['pay_type'].'成功';
                    $partial_amount=OrderLeasePartial::where('out_order_no',$item['trade_no'])->value('partial_amount');
                    if($partial_amount){$log['data'][$k]['failReason'] = '部分还款:'.$partial_amount;}
                    if($item['merchant_contribute']>0){
                        $log['data'][$k]['rent']=$log['data'][$k]['rent'].'(使用优惠券:'.$item['merchant_contribute'].')';
//                        dd(json_decode(json_decode($item['result'],true)['voucher_detail_list'],true));
                        $log['data'][$k]['failReason'] = '使用优惠券:'.json_decode(json_decode($item['result'],true)['voucher_detail_list'],true)[0]['name'];
                    }
                }else{
                    $log['data'][$k]['pay_type'] = $log['data'][$k]['pay_type'].'失败';
                    $log['data'][$k]['failReason'] = '余额不足';
                }
                $log['data'][$k]['status'] = OtherWithholdLog::STATUS_LIST[$item['status']];
            }
        }
        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'获取成功',$log);

    }

    /**
     * 获取所有订单状态
     * @return false|string
     */
    public function getOrderStatus(){
        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'获取成功',OrderService::ORDER_STATUS_NAME_LIST);
    }

    /**
     * 获取账单支付流水
     * @param Request $request
     * @return false|string
     */
    public function getLeaseFlow(Request $request){
        $orderId = $request->input('orderId','');
        $leaseId = $request->input('leaseId','');
        if(!$orderId || !$leaseId){
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'参数错误');
        }
        if(!Order::checkOrderById($orderId)){
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'未找到订单信息');
        }
        $flow = OtherWithholdLog::getFlow('',$leaseId);
        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'获取成功',$flow);
    }

    /**
     * 财务账单列表
     * @param Request $request
     * @return false|string
     */
    public function financeLeaseList(Request $request){
        $data = Order::financeLeaseList($request);
        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'获取成功',$data['list']);
    }


    /**
     * 花呗财务账单列表
     * @param Request $request
     * @return false|string
     */
    public function financeHuaBeiList(Request $request){
        $data = Order::financeHuaBeiList($request);
        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'获取成功',$data['list']);
    }

    /**
     * 财务逾期列表
     * @param Request $request
     * @return false|string
     */
    public function financeOverdueList(Request $request){
        $data = Order::financeOverdueList($request);
        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'获取成功',['count'=>$data['count'],'list'=>$data['list']]);
    }

    /**
     * 财务对账流水
     * @param Request $request
     * @return false|string
     */
    public function reconciliationFlow(Request $request){
        $data = Order::reconciliationFlow($request);
        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'获取成功',['count'=>$data['count'],'list'=>$data['list']]);
    }

    /**
     * 获取手续费率和手续费详情
     * @param Request $request
     * @return false|string
     */
    public function getFeeDetails(Request $request){
        $orderId = $request->input('orderId','');
        $transId = $request->input('transId','');
        if(!$orderId || !$transId){
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'参数错误');
        }
        if(!Order::checkOrderById($orderId)){
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'未找到订单信息');
        }
        $flow = TransDetail::from('sp_trans_detail as td')->leftjoin('sp_shop_trans as st','td.trans_id','st.id')->where(['td.trans_id'=>$transId])->select('td.*','st.order_rate','st.order_fixed_cost','st.trans_type')->first();
        $flow = obToArray($flow);
        if($flow){
            if($flow['order_lease_id']){
                $lease = OrderLease::where(['id'=>$flow['order_lease_id']])->select('rent','alipay_trade_no as trade_no')->first();
            }
            $pay_type = Order::where(['id'=>$orderId])->value('pay_type');
            if($pay_type == Order::ORDER_PAY_TYPE_HUABEI){
                $lease = Order::where(['id'=>$orderId])->select('rent_total as rent','alipay_trade_no as trade_no')->first();
            }
            if($flow['trans_type'] == ShopTrans::ORDER_TRANS_TYPE_BUYOUT){
                $lease = OrderBuyout::where(['order_id'=>$orderId])->select('rent','alipay_trade_no as trade_no')->first();
            }
            $lease = obToArray($lease);
            $flow['rent'] = isset($lease['rent'])  ? $lease['rent'] : '';
            $flow['trade_no'] = isset($lease['trade_no'])  ? $lease['trade_no'] : '';
        }
        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'获取成功',obToArray($flow));
    }

    /**
     * 自动代扣开关操作
     * @param Request $request
     * @return false|string
     */
    public function withholdSwitch(Request $request){
        $orderId = $request->input('orderId','');
        $leaseId = $request->input('leaseId','');
        $withholdStatus = $request->input('withholdStatus','');
        if(!$orderId || !$leaseId || !$withholdStatus){
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'参数错误');
        }
        if(!Order::checkOrderById($orderId)){
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'未找到订单信息');
        }
        if(!in_array($withholdStatus,[OrderLease::LEASE_WITHHOLD_STATUS_NO,OrderLease::LEASE_WITHHOLD_STATUS_OFF])){
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'状态异常');
        }
        $res = OrderLease::where(['id'=>$leaseId])->update(['withhold_status'=>$withholdStatus]);
        if($res){
            return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'操作成功');
        }else{
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'操作失败');
        }
    }

    /**
     * 手动代扣
     * @param Request $request
     * @return false|string
     * @throws \Exception
     */
    public function manualWithholding(Request $request){
        $orderId = $request->input('orderId','');
        $leaseId = $request->input('leaseId','');
        if(!$orderId || !$leaseId){
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'参数错误');
        }
        if(!Order::checkOrderById($orderId)){
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'未找到订单信息');
        }


        $where  = [
            ['id',$leaseId],
            ['order_id',$orderId],
            ['pay_type',OrderLease::LEASE_PAY_TYPE_ANTCHAIN],
        ];
        $lease = OrderLease::where($where)->whereIn('status',[OrderLease::ORDER_LEASE_STATUS_NOPAY,OrderLease::ORDER_LEASE_STATUS_OVERDUE])->first();
        $lease = obToArray($lease);
        if(!$lease){
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'未找到代扣账单');
        }
        $orderStatus = Order::where(['id'=>$orderId])->value('status');
        if(!OrderLease::withholdLimit($orderStatus,$lease['withhold_status'],$lease['begin_time'],$lease['status'])){
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'此账单不符合代扣条件');
        }

        //数据
        $data = Order::from('sp_order_2 as o')
            ->select('o.trade_no','o.rights_auth_no','o.payer_user_id','o.flow_id','ol.id as leaseId','ol.which_period',
                'ol.begin_time','ol.rent','ol.status','m.user_id')
            ->leftjoin('sp_order_lease_2 as ol' ,'o.id','ol.order_id')
            ->leftjoin('sp_member_2 as m' ,'o.uid','m.id')
            ->where('ol.id',$leaseId)
            ->where('m.user_id','<>',0)
            ->first();
        $data = obToArray($data);

        $res= AntchainAtoServicejuzu::retryWithholdPlan($data['trade_no'],$data['which_period']);
        if($res['resultCode']=='OK'){
            return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'重试代扣成功');
        }else{
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,$res['resultMsg']);
        }

    }

    /**
     * 财务订单列表导出
     * @param Request $request
     * @return false|string
     */
    public function orderListExport(Request $request){
        $data = ExportService::adminFinanceOrderListExport($request);
        if(!$data){
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'无数据');
        }
        $params = $request->input();
        if(isset($params['orderStatus']) && $params['orderStatus']){
            $params['orderStatus'] = OrderService::ORDER_STATUS_NAME_LIST[$params['orderStatus']];
        }
        ExportService::exportFile($params,$data,ExportService::FILE_ORDER_FINANCE_LIST);
    }

    /**
     * 财务账单列表导出
     * @param Request $request
     * @return false|string
     */
    public function leaseListExport(Request $request){
        $data = ExportService::adminFinanceLeaseListExport($request);
        if(!$data){
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'无数据');
        }
        $params = $request->input();
        if(isset($params['orderStatus']) && $params['orderStatus']){
            $params['orderStatus'] = OrderService::ORDER_STATUS_NAME_LIST[$params['orderStatus']];
        }
        ExportService::exportFile($params,$data,ExportService::FILE_LEASE_FINANCE_LIST);
    }

    /**
     * 财务逾期列表导出
     * @param Request $request
     * @return false|string
     */
    public function overdueListExport(Request $request){
        $data = ExportService::adminFinanceOverdueListExport($request);
        if(!$data){
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'无数据');
        }
        $params = $request->input();
        if(isset($params['sendStatus']) && $params['sendStatus']){
            $params['sendStatus'] = OrderLease::ORDER_LEASE_OVERDUE_SEND_LIST[$params['sendStatus']];
        }
        ExportService::exportFile($params,$data,ExportService::FILE_OVERDUE_FINANCE_LIST);
    }

    /**
     * 财务对账流水导出
     * @param Request $request
     * @return false|string
     */
    public function reconciliationExport(Request $request){
        $data = ExportService::adminFinanceReconciliationExport($request);
        if(!$data){
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'无数据');
        }
        $params = $request->input();
        if(isset($params['moneyFlow']) && $params['moneyFlow']){
            $params['moneyFlow'] = $params['moneyFlow'] == 1 ? '收入' : '支出';
        }
        ExportService::exportFile($params,$data,ExportService::FILE_RECORD_FINANCE_LIST);
    }

    /**
     * 财务花呗账单导出
     * @param Request $request
     * @return false|string
     */
    public function huaBeiListExport(Request $request){
        $data = ExportService::adminFinanceHuaBeiListExport($request);
        if(!$data){
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'无数据');
        }
        $params = $request->input();
        if(isset($params['orderStatus']) && $params['orderStatus']){
            $params['orderStatus'] = OrderService::ORDER_STATUS_NAME_LIST[$params['orderStatus']];
        }
        ExportService::exportFile($params,$data,ExportService::FILE_HUABEI_ORDER_FINANCE_LIST);
    }

    /**
     * 获取客服备注
     * @param Request $request
     * @return false|string
     */
    public function getOrderRemark(Request $request){
        $orderId = $request->input('orderId','');
        $page = $request->input('page',1);
        $pageSize = $request->input('pageSize',10);
        if(!$orderId){
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'参数错误');
        }
        if(!Order::checkOrderById($orderId)){
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'未找到订单信息');
        }
        $remark = OrderRemark::where(['order_id'=>$orderId])->select('username','create_time','remark')->orderBy('id','desc')->paginate($pageSize);
        $remark = obToArray($remark);
        if(isset($remark['data'])){
            foreach ($remark['data'] as $k=>$v){
                $remark['data'][$k]['create_time'] = dateFormat($v['create_time']);
            }
        }
        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'获取成功',$remark);
    }

    /**
     * 发送逾期短信
     * @param Request $request
     * @return false|string
     */
    public function sendOverdueSms(Request $request){
        $leaseId = $request->input('leaseId','');
        if(!$leaseId){
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'参数错误');
        }
        $leaseId = explode(',',$leaseId);
        if(!is_array($leaseId)){
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'租期格式错误');
        }
        $orderLease = Order::from('sp_order_2 as o')
            ->leftjoin('sp_order_lease_2 as ol','o.id','ol.order_id')
            ->leftjoin('sp_order_goods_2 as og','o.id','og.order_id')
            ->leftjoin('sp_member_2 as m','o.uid','m.id')
        ->where(['ol.status'=>OrderLease::ORDER_LEASE_STATUS_OVERDUE])
        ->whereIn('ol.id',$leaseId)
        ->select(
            'o.seller_id','o.trade_no'
            ,'ol.id','ol.rent','ol.begin_time','ol.which_period'
            ,'og.goods_title'
            ,'m.cert_name','m.mobile'
        )->get();
        $orderLease = obToArray($orderLease);
        $start_date = date('Y-m-01 00:00:00', strtotime(date('Y-m')));
        $end_date = date('Y-m-d 23:59:59', strtotime("$start_date +1 month -1 day"));
        foreach ($orderLease as $item){
            $where[] = ['create_time','>=',$start_date];
            $where[] = ['create_time','<=',$end_date];
            $where[] = ['template_id',SmsTemplate::SMS_TEMPLATE_ORDER_COLLECTION];
            $where[] = ['lease_id',$item['id']];
            $log = SmsRecord::where($where)->orderBy('id','desc')->value('id');
            if(empty($log)){
                //发送短信
                $res = ShopSms::orderOverdueCollection($item['seller_id'],$item['cert_name'],$item['goods_title'],$item['rent'],date('Y-m-d H:i:s',$item['begin_time']),$item['which_period'],$item['trade_no'],$item['mobile'],$this->users['id'],$item['id']);
                if($res['status']  == CommonService::HTTP_CODE_SUCCESS){
                    OrderLease::where(['id'=>$item['id']])->update(['msg_status'=>OrderLease::ORDER_LEASE_OVERDUE_SEND_YES]);
                }
            }
        }
        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'已操作');
    }

    /**
     * 获取逾期短信记录
     * @param Request $request
     * @return false|string
     */
    public function getOverdueSmsLog(Request $request){
        $leaseId = $request->input('leaseId','');
        if(!$leaseId){
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'参数错误');
        }
        $where[] = ['template_id',SmsTemplate::SMS_TEMPLATE_ORDER_COLLECTION];
        $where[] = ['lease_id',$leaseId];
        $log = SmsRecord::where($where)->select('id','content','send_status','reason','create_time')->get();
        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'获取成功',obToArray($log));
    }

    /**
     * 设置客服备注
     * @param Request $request
     * @return false|string
     */
    public function setOrderRemark(Request $request){
        try{
            $orderId = $request->input('orderId','');
            $remark = $request->input('remark','');
            if(!$orderId || !$remark){
                return ReturnMsg(CommonService::HTTP_CODE_FAIL,'参数错误');
            }
            if(!Order::checkOrderById($orderId)){
                return ReturnMsg(CommonService::HTTP_CODE_FAIL,'未找到订单信息');
            }
            $add = [
                'uid'=>$this->users['id'],
                'username'=>$this->users['username'],
                'create_time'=>time(),
                'remark'=>$remark,
                'order_id'=>$orderId,
            ];
            OrderRemark::insert($add);
            return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'添加成功');
        }catch (\Exception $exception){
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'添加失败');
        }
    }

    /**
     * 财务回款列表
     * @param Request $request
     * @return false|string
     */
    public function financeCollectionList(Request $request){
        $data = Order::financeCollectionList($request);
        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'获取成功',$data['list']);
    }

    /**
     * 财务回款列表导出
     * @param Request $request
     * @return false|string
     */
    public function collectionListExport(Request $request){
        $data = ExportService::adminFinanceCollectionExport($request);
        if(!$data){
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'无数据');
        }
        $params = $request->input();
        if(isset($params['orderStatus']) && $params['orderStatus']){
            $params['orderStatus'] = OrderService::ORDER_STATUS_NAME_LIST[$params['orderStatus']];
        }
        ExportService::exportFile($params,$data,ExportService::FILE_COLLECTION_ORDER_FINANCE_LIST);
    }

    /**
     * 设备分期列表
     * @param Request $request
     * @return false|string
     */
    public function financeEquipmentList(Request $request){
        $data = Order::financeEquipmentList($request);
        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'获取成功',$data['list']);
    }

    /**
     * 设备分期列表导出
     * @param Request $request
     * @return false|string
     */
    public function equipmentListExport(Request $request){
        $data = ExportService::adminFinanceEquipmentExport($request);
        $params = $request->input();
        if(isset($params['orderStatus']) && $params['orderStatus']){
            $params['orderStatus'] = OrderService::ORDER_STATUS_NAME_LIST[$params['orderStatus']];
        }
        ExportService::exportFile($params,$data,ExportService::FILE_EQUIPMENT_ORDER_FINANCE_LIST);
    }


    /**
     * 获取修改账单为线下支付订单信息
     */
    public function getOfflinePayData(Request $request){
        $order_id = $request->input('order_id','');
        $lease_id = $request->input('lease_id','');
        if(!empty($order_id) && !empty($lease_id)){
            $order = OrderLease::from('sp_order_lease_2 as ol')->leftjoin('sp_order_2 as o','o.id','ol.order_id')->where([['ol.order_id',$order_id],['ol.id',$lease_id]])->select('o.trade_no','ol.which_period','o.seller_id')->first();
            $order = obToArray($order);
            if(!empty($order)){
                $send_mobile = ShopSms::where([['member_id',$order['seller_id']],['template_id',SmsTemplate::SMS_TEMPLATE_OFFLINE_PAY_CODE],['status',ChargeService::SHOP_SMS_STATUS_OPEN]])->value('send_mobile');
                if($send_mobile){
                    $order['mobile'] = $send_mobile;
                    unset($order['seller_id']);
                    return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'获取成功',$order);
                }else{
                    return ReturnMsg(CommonService::HTTP_CODE_FAIL,'验证码短信模板未开启，请先前往营销-增值服务-短信提醒开通短信模板');
                }
            }else{
                return ReturnMsg(CommonService::HTTP_CODE_FAIL,'订单不存在');
            }
        }else{
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'请选择要操作的订单和租期');
        }
    }

    /**
     * 线下支付发送验证码
     */
    public function offlinePaySendCode(Request $request){
        $order_id = $request->input('order_id','');
        $lease_id = $request->input('lease_id','');
        if(!empty($order_id) && !empty($lease_id)){
            $order = OrderLease::from('sp_order_lease_2 as ol')->leftjoin('sp_order_2 as o','o.id','ol.order_id')->where([['ol.order_id',$order_id],['ol.id',$lease_id]])->select('o.trade_no','ol.which_period','o.seller_id')->first();
            $order = obToArray($order);
            if(!empty($order)){
                $send_type = SmsService::SMS_SEND_TYPE_OFFLINE;
                $send_mobile = ShopSms::where([['member_id',$order['seller_id']],['template_id',SmsTemplate::SMS_TEMPLATE_OFFLINE_PAY_CODE],['status',ChargeService::SHOP_SMS_STATUS_OPEN]])->value('send_mobile');
                $code = rand(100000,999999);
                $res = ShopSms::orderUpdateOfflinePay($order['seller_id'],$order['trade_no'],$order['which_period'],$code);
                if($res['status'] == CommonService::HTTP_CODE_SUCCESS){
                    Redis::setEx($send_mobile.$send_type,300,$code);
                    return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'发送成功');
                }else{
                    return ReturnMsg(CommonService::HTTP_CODE_FAIL,$res['msg']);
                }
            }else{
                return ReturnMsg(CommonService::HTTP_CODE_FAIL,'订单不存在');
            }
        }else{
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'缺少参数');
        }
    }

    /**
     * 修改账单为线下支付
     */
    public function setOfflinePay(Request $request){
        $order_id = $request->input('order_id','');
        $lease_id = $request->input('lease_id','');
        $sms_code = $request->input('sms_code','');
        if(!empty($order_id) && !empty($lease_id) && !empty($sms_code)){
            $order = OrderLease::from('sp_order_lease_2 as ol')->leftjoin('sp_order_2 as o','o.id','ol.order_id')->where([['ol.order_id',$order_id],['ol.id',$lease_id]])->select('o.uid','o.trade_no','ol.which_period','o.seller_id')->first();
            $order = obToArray($order);
            if(!empty($order)){
                $send_type = SmsService::SMS_SEND_TYPE_OFFLINE;
                $send_mobile = ShopSms::where([['member_id',$order['seller_id']],['template_id',SmsTemplate::SMS_TEMPLATE_OFFLINE_PAY_CODE],['status',ChargeService::SHOP_SMS_STATUS_OPEN]])->value('send_mobile');
                if(Redis::get($send_mobile.$send_type) == $sms_code){
                    $lease_res = OrderLease::where([['order_id',$order_id],['id',$lease_id]])->update(['status'=>OrderLease::ORDER_LEASE_STATUS_OFFLINEPAY]);
                    $log_content = '修改订单'.$order['trade_no'].'第'.$order['which_period'].'期状态为线下支付。';
                    $log_res = OrderLog::addOrderLog($order_id,$this->users['id'],OrderLog::ORDER_LOG_OPERATE_TYPE_ADMIN,$log_content);
                    if($lease_res !==false && $log_res!==false){
                        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'修改成功');
                    }else{
                        return ReturnMsg(CommonService::HTTP_CODE_FAIL,'修改失败');
                    }
                }else{
                    return ReturnMsg(CommonService::HTTP_CODE_FAIL,'验证码错误');
                }
            }else{
                return ReturnMsg(CommonService::HTTP_CODE_FAIL,'订单不存在');
            }
        }else{
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'缺少参数');
        }
    }


    /**
     * 获取订单操作记录
     */
    public function getOrderLog(Request $request){
        $order_id = $request->input('order_id','');
        $page = $request->input('page',1);
        $pageSize = $request->input('pageSize',20);
        if(!$order_id){
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'参数错误');
        }
        $orderLog = OrderLog::getOrderLogByOrderIdPage($order_id,$page,$pageSize);
        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'获取成功',$orderLog);
    }

}
